home *** CD-ROM | disk | FTP | other *** search
- /*
- * Initialization segment for the KEYMAP program
- * This code is discarded after it is used.
- *
- * Copyright (c) 1991 by Peter Belew and William S. Hall
- */
-
- #define NOCOMM
- #define NOKANJI
- #define NOATOM
- #define NOSOUND
- #include <windows.h>
- #include <string.h>
- #include "keymap.h"
- #include "winutils.h"
-
- /* local function declarations */
- static BOOL NEAR RegisterWindowClass(HANDLE);
- static void NEAR GetPrevInstanceData(HANDLE);
- static HWND NEAR MakeAndShowMainWnd(HANDLE, HANDLE, int);
- static BOOL NEAR RegisterKeyClass(HANDLE hInstance);
-
- /* local variable */
- char *keyclass = "KEYWINDOW";
-
- /* This routine is FAR since it is called from another segment */
- BOOL FAR InitProgram(HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int cmdShow)
- {
- hInst = hInstance;
-
- /* if this is the first instance of the program ... */
- if (!hPrevInstance) {
- /* register the window */
- if (!RegisterWindowClass(hInstance))
- return FALSE;
- if (!RegisterKeyClass(hInstance))
- return FALSE;
- }
-
- /* A previous instance exists so get some data from there */
- else
- GetPrevInstanceData(hPrevInstance);
-
- /* Create and show the window */
- if ((hWndMain=MakeAndShowMainWnd(hInstance,hPrevInstance,cmdShow)) == NULL)
- return FALSE;
-
- return TRUE;
- }
-
- /* Register main window class */
- static BOOL NEAR RegisterWindowClass(HANDLE hInstance)
- {
-
- WNDCLASS WndClass;
-
- /* Load the name string from resources */
- LoadString(hInstance,IDS_APPNAME,szAppName,sizeof(szAppName));
- memset(&WndClass, 0, sizeof(WndClass));
-
- /* fill the structure */
- WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- WndClass.hIcon = LoadIcon(hInstance, szAppName);
- WndClass.lpszMenuName = (LPSTR)szAppName;
- WndClass.lpszClassName = (LPSTR)szAppName;
- WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- WndClass.hInstance = hInstance;
- WndClass.style = CS_VREDRAW | CS_HREDRAW;
- WndClass.lpfnWndProc = MainWndProc;
-
- return RegisterClass(&WndClass);
- }
-
- /* register key window class */
- static BOOL NEAR RegisterKeyClass(HANDLE hInstance)
- {
-
- WNDCLASS WndClass;
-
- memset(&WndClass, 0, sizeof(WndClass));
-
- /* fill the structure */
- WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- WndClass.hIcon = NULL;
- WndClass.lpszMenuName = NULL;
- WndClass.lpszClassName = (LPSTR)keyclass;
- WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- WndClass.hInstance = hInstance;
- WndClass.style = CS_VREDRAW | CS_HREDRAW;
- WndClass.lpfnWndProc = KeyWndProc;
-
- return RegisterClass(&WndClass);
- }
-
- /* If this not the first instance, we can retrieve static data
- from a previous invocation of the program
- */
- static void NEAR GetPrevInstanceData(HANDLE hInstance)
- {
- GetInstanceData(hInstance, szAppName, sizeof(szAppName));
- }
-
- /* Create the window, making sure that its position and size
- are suitable for the display.
- */
- static HWND NEAR MakeAndShowMainWnd(HANDLE hInstance,
- HANDLE hPrevInstance,int cmdShow)
- {
-
- char szTitle[50];
- HWND hWnd;
-
- LoadString(hInstance, IDS_TITLE, szTitle, sizeof(szTitle));
-
- /* create the window, resize later */
- hWnd = CreateWindow(szAppName,
- szTitle,
- WS_OVERLAPPEDWINDOW,
- 0,0,0,0,
- NULL,
- NULL,
- hInstance,
- NULL);
-
- /* if successful, display the window and show success */
- if (hWnd) {
- ShowWindow(hWnd, cmdShow);
- UpdateWindow(hWnd);
- }
- return hWnd;
- }
-
- /* Generate the key windows */
- void FAR MainWndCreate(HWND hWnd)
- {
- register int i;
- HDC hDC;
- TEXTMETRIC tm;
- int x, y;
- int cx, cy;
- int offset;
- int sc;
- DWORD tl;
- HWND hChild;
-
- KeyColor = IDM_GRAY; /* set the key color variable */
-
- hDC = GetDC(hWnd); /* get system font size */
- tl = GetTextExtent(hDC, "@ @", 3); /* get extent of some fat characters */
- GetTextMetrics(hDC, &tm);
- ReleaseDC(hWnd, hDC);
- cwidth = tm.tmAveCharWidth; /* save width and height */
- cheight = tm.tmHeight + tm.tmExternalLeading;
-
- cx = LOWORD(tl); /* try to be device independent */
- cxborder = GetSystemMetrics(SM_CXBORDER);
- cyborder = GetSystemMetrics(SM_CYBORDER);
- cy = 2 * cheight + 4 * cyborder;
- x = cx + cwidth / 2;
-
- /* Create the key windows. Use the keyboard scan code as the
- key window id. Lots of magic numbers here as we use our
- knowledge of the extended keyboard scan codes to do
- the right thing */
- for (i = 0; i < MAXKEYS ; i++) {
- switch (i) {
- case 0: /* first letter key */
- sc = 41;
- y = cheight;
- offset = cwidth / 2;
- break;
- case 1: /* start in with the numbers */
- sc = 2;
- break;
- case 13: /* moving to next row */
- offset = x + cx / 2 - i * x;
- y += cy + cheight / 2;
- sc = 16;
- break;
- case 25: /* going to third row */
- offset = x + cx - i * x;
- y += cy + cheight / 2;
- sc = 30;
- break;
- case 36: /* fourth row */
- sc = 43;
- break;
- case 37: /* 102nd key */
- offset = x + cx / 2 - i * x;
- y += cy + cheight / 2;
- sc = 86;
- break;
- case 38: /* finish 4th row */
- sc = 44;
- break;
- }
- kdata[i].scancode = sc++;
- hChild = CreateWindow(keyclass,
- "",
- WS_CHILD | WS_VISIBLE,
- offset + i * x,
- y,
- cx, cy,
- hWnd,
- kdata[i].scancode,
- hInst,
- NULL);
- if (hChild == NULL) { /* failed */
- PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
- return;
- }
- }
- y += cy + cheight;
- hEdit = CreateWindow("EDIT", /* create the text window */
- "",
- WS_CHILD | WS_VISIBLE | WS_BORDER |
- ES_MULTILINE | WS_VSCROLL | ES_AUTOVSCROLL,
- cwidth / 2,
- y,
- 14 * x - cwidth,
- 6 * cheight,
- hWnd,
- 1000,
- hInst,
- NULL);
-
- if (hEdit) { /* subclass the window */
- fpLocalEditWndProc =
- MakeProcInstance((FARPROC)LocalEditWndProc,hInst);
- if (fpLocalEditWndProc)
- fpEditWndProc = (FARPROC)SetWindowLong(hEdit,
- GWL_WNDPROC,
- (LONG)fpLocalEditWndProc);
- }
- else { /* failed */
- PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
- return;
- }
- /* now resize the main window */
- MoveWindow(hWnd, GetSystemMetrics(SM_CXSIZE),
- GetSystemMetrics(SM_CYSIZE),
- 14 * x + cwidth, y + 10 * cheight, TRUE);
- ModifyWindowsTitle(hWnd); /* fix up the title with the keyboard name */
- }
-